package com.learning.spring.optimize.taglib;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 * ClassName: TaglibBeanDefinitionParser
 * Description: 标签的解析类
 * Date: 2018/8/20 10:53 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class TaglibBeanDefinitionParser implements BeanDefinitionParser {
    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {

        // 通过 BeanDefinitionBuilder 创建 Bean 定义
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(TaglibBean.class);

        // 获取自定义标签的属性
        String name = element.getAttribute("name");
        builder.addPropertyReference("name", name);
        AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();

        // 注册 Bean 的定义
        BeanComponentDefinition beanComponentDefinition = new BeanComponentDefinition(beanDefinition, "taglibBean");
        parserContext.registerBeanComponent(beanComponentDefinition);
        return beanDefinition;
    }
}
